# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Maintainer: Michael Pirogov <vbnet.ru@gmail.com>
pkgname=loki
pkgver=2.3.0
pkgrel=4
pkgdesc="Like Prometheus, but for logs"
options="net"
url="https://github.com/grafana/loki"
license="Apache-2.0"
arch="x86_64 aarch64 ppc64le riscv64" # limited by 64 bit
makedepends="go bash libc-dev"
checkdepends="tzdata"
subpackages="$pkgname-promtail-openrc:promtail_openrc $pkgname-promtail $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/grafana/loki/archive/refs/tags/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-promtail.confd
	$pkgname-promtail.initd
	go-1.18-sigsegv.patch
	"

export GOPATH="$srcdir"
export GOCACHE="$srcdir"
export GOTMPDIR="$srcdir"
export GOFLAGS="$GOFLAGS -modcacherw -trimpath"
export CGO_ENABLED=0

build() {
	make GOMOD=readonly logcli loki promtail
}

check() {
	# not stable test for fluent-bit
	# shell check suggests to break it
	# shellcheck disable=SC2046
	TZ=UTC GOGC=10 go test -covermode=atomic -coverprofile=coverage.txt -p=4 $(go list ./... | grep -v fluent-bit)
}

promtail() {
	pkgdesc="Promtail client"
	mkdir -p "$subpkgdir"/etc/$pkgname
	cp "$builddir"/clients/cmd/promtail/promtail-local-config.yaml "$subpkgdir"/etc/$pkgname
	install -Dm0755 "$builddir"/clients/cmd/promtail/promtail -t "$subpkgdir"/usr/bin
}

promtail_openrc() {
	pkgdesc="Promtail client (Open RC init scripts)"
	install_if="openrc loki-promtail=$pkgver-r$pkgrel"

	install -Dm755 "$srcdir"/$pkgname-promtail.initd "$subpkgdir"/etc/init.d/$pkgname-promtail
	install -Dm644 "$srcdir"/$pkgname-promtail.confd "$subpkgdir"/etc/conf.d/$pkgname-promtail
}

package() {
	mkdir -p "$pkgdir"/etc/$pkgname
	cp "$builddir"/cmd/loki/loki-local-config.yaml "$pkgdir"/etc/$pkgname
	install -Dm0755 cmd/$pkgname/$pkgname -t "$pkgdir"/usr/bin
	install -Dm0755 cmd/logcli/logcli -t "$pkgdir"/usr/bin

	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
7f2855d8df69c551e182d0e27e2a6fb6960fa5bc525dcc90cb2dc2428f52aeb5e2bddd0a9232e1ab70cbe60a9d5bb3349ba749dbee82fc63c6fc43e61240feed  loki-2.3.0.tar.gz
469920d90daebf142db5dc5d3f2e972f59f5f5dc86684c4dcd5924a33ac7db305561006050ad42393296977ea27f1ce1cd78f78c71de351335167b41e4631a85  loki.initd
b4c281e191e7620ffe19d3fcc0b5669f54051a8108f0abc5b1aec2f2026452e02e3c26c3885e1e857cb6007bbc41c1f964e6d25d73bdfe170b67b63ecd9f14fd  loki.confd
f7d33f03d51c3cc5cda082e72f56ec821c60f70f42923a215652517835b66a68d7db3b246cd1a53ce0b2c003fe31f7e6cca06965ba792e92117d7e91abce829f  loki-promtail.confd
ff72431ba978880cc58914302b2ec1e48ef3d16b2a667dc82deaa3d2d890331170757dcac48ba0b145c988e70e85c240c94da335894d2c9d114fa811007f1111  loki-promtail.initd
e0f881b3b470fa77eb242efe8c0054bf249f2099face6c2e66659351b8dd1d196e9d4a11fed59b38225e411d72b81f4f51d510335773885e30917e59a0ef786d  go-1.18-sigsegv.patch
"
